/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uniq.servlet;

import com.uniq.db.ConnectionFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author user
 */
@WebServlet(name = "Exchange", urlPatterns = {"/Exchange"})
public class Exchange extends HttpServlet {
  

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
     
            String branch=request.getParameter("branch");
            String quantity=request.getParameter("quantity");
            String source=request.getParameter("source");
            String selected_product=request.getParameter("selected_product");
            
            
            String srcquantity="SELECT * FROM stock WHERE id='"+selected_product+"' AND branch='"+source+"'";
            int src_quantity=0;
            Connection con=ConnectionFactory.getConnection();
            Statement s=con.createStatement();
             int id=0;
             String supplier=new String();
             String brand=new String();
             String size=new String();
             String color=new String();
             String article_no=new String();
             String cost_price=new String();
             String description=new String();
             String selling_price=new String();
             Blob qrcode = null;
             Blob product_image = null;
             
             
            
            ResultSet rs=s.executeQuery(srcquantity);
            if(rs.next())
            {
                id=rs.getInt("id");
                supplier=rs.getString("supplier");
                brand=rs.getString("brand");
                size=rs.getString("size");
                color=rs.getString("color");
                article_no=rs.getString("article_no");
                cost_price=rs.getString("cost_price");
                description=rs.getString("description");
                selling_price=rs.getString("selling_price");
                qrcode=rs.getBlob("qrcode");
                product_image=rs.getBlob("product_image");
       src_quantity=rs.getInt("stock_count");
            }
            
            int updatedValue=src_quantity-Integer.parseInt(quantity);
            String updateQuantity="UPDATE stock SET stock_count="+updatedValue+" WHERE id='"+selected_product+"' AND branch='"+source+"'";  
          
          
            Statement s1=con.createStatement();
            s1.execute(updateQuantity);
            
           PreparedStatement ps=con.prepareStatement("insert into stock(id,supplier,brand,size,color,article_no,cost_price,description,selling_price,qrcode,product_image,branch,stock_count) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
            
            
            ps.setInt(1,id);
            ps.setString(2,supplier);
            ps.setString(3,brand);
            ps.setString(4,size);
            ps.setString(5,color);
            ps.setString(6,article_no);
            ps.setString(7,cost_price);
            ps.setString(8,description);
            ps.setString(9,selling_price);
            ps.setBlob(10,qrcode);
            ps.setBlob(11,product_image);
            ps.setString(12, branch);
            ps.setString(13, quantity);
            ps.executeUpdate();
            
         out.println("Click here to Exchange again<a href='StockExchange.jsp'> Exchange stock </a>");   
            
            
        } catch (SQLException ex) {
            Logger.getLogger(Exchange.class.getName()).log(Level.SEVERE, null, ex);
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
